package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.project.system.domain.SiyuQuery;
import com.ruoyi.project.system.service.ISiyuQueryService;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.ByteArrayOutputStream;
import static com.ruoyi.common.utils.file.FileUtils.makeAithFile;

/**
 * 首页
 *
 * @author ruoyi
 */
@RestController
public class SysIndexController {
    /**
     * 系统基础配置
     */
    @Autowired
    private RuoYiConfig ruoyiConfig;


    @Autowired
    private ISiyuQueryService siyuQueryService;

    /**
     * 访问首页，提示语
     */
    @RequestMapping("/")
    public String index() {
        return StringUtils.format("欢迎使用{}后台管理框架，当前版本：v{}，请通过前端地址访问。", ruoyiConfig.getName(), ruoyiConfig.getVersion());
    }

    @Anonymous
    @ResponseBody
    @RequestMapping(value ="/authBook", produces = MediaType.APPLICATION_PDF_VALUE)
    public byte[] authBook(Integer queryId) {
        Document document;
        if (queryId == null) {
            document = makeAithFile(new SiyuQuery("您的姓名", "您的身份证号"));
        } else {
            SiyuQuery siyuQuery = siyuQueryService.selectSiyuQueryByQueryId(queryId);
            if (siyuQuery == null) {
                document = makeAithFile(new SiyuQuery("您的姓名", "您的身份证号"));
            } else {
                document = makeAithFile(siyuQuery);
            }
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        document.saveToStream(outputStream, FileFormat.PDF);
        return outputStream.toByteArray();
    }

}
